<?php
ob_start();
session_start();
require("includes/basic.php");
$user = $_POST['username'];
$password = $_POST['password'];

$query1  = "SELECT * FROM customer_master where record_status='Y' and user_name='".trim($user)."'";
$result1 = mysql_query($query1);

if(mysql_num_rows($result1)!="")
{ 
	while($row1 = mysql_fetch_assoc($result1))
	{

		if(trim(md5($password))==trim($row1['password']))
		{ 
			
			$_SESSION['uid']= $row1['customer_id'];
			$_SESSION['name']= $row1['customer'];
			$_SESSION['password']= $row1['password'];
			$_SESSION['usertype']= "O";
			$_SESSION['username']=$row1['user_name'];
			$_SESSION['customerid']=$row1['customer_id'];
			$_SESSION['customer']=$row1['customer'];
			$_SESSION['usertypeid']=$row1['usertypeid'];
			//if(isset($_SESSION['url'])) 
			  // $url = $_SESSION['url']; // holds url for last page visited.
			   //echo $url;
			//else 
			   $url = "index.php"; // default page for 

			header("Location: $url"); // perform correct redirect.

			exit;
		 } 
		 else
		 { 
		 header('Location:index.php?er=1');
		 }
	 }  
}
else
{ 

			$query2  = "SELECT * FROM user_master where record_status='Y' and user_name='".trim($user)."'";
			$result2 = mysql_query($query2);
			if(mysql_num_rows($result2)!="")
			{ 
			
				while($row2 = mysql_fetch_assoc($result2))
				{
					
					if(trim(md5($password))==trim($row2['user_password']))
					{ 
						$_SESSION['uid']= $row2['user_id'];
						$_SESSION['name']= $row2['first_name'];
						$_SESSION['password']= $row2['user_password'];
						$_SESSION['username']=$row2['user_name'];
						$_SESSION['customerid']=$row2['customer_id'];
						$_SESSION['usertypeid']=$row2['usertypeid'];
						
						if($user=='devi@catseyetech.in')
							{
						$_SESSION['usertype']= 'A';
						$url = "index.php";
							}
							else
							{
						$_SESSION['usertype']= 'D';
						$_SESSION['customerid']= $row2['customer_id'];
						$url = "index.php"; // default page for
							}
						header("Location: $url"); // perform correct redirect.

						exit;
					 } 
					
					 else
					 { 
						header('Location:index.php?er=1');
					 }
				 }  
			}
			 else
			{ 
				$query4  = "SELECT * FROM vendor_master where record_status='Y' and user_name='".trim($user)."'";
				$result4 = mysql_query($query4);
				if(mysql_num_rows($result4)!="")
				{ 
					while($row4 = mysql_fetch_assoc($result4))
					{



						if(trim(md5($password))==trim($row4['password']))


						{ 
							$_SESSION['uid']= $row4['vendor_id'];
							$_SESSION['name']= $row4['vendor'];
							$_SESSION['password']= $row4['password'];
							$_SESSION['username']=$row4['user_name'];
							//$_SESSION['customerid']=$row2['vendor_created_by'];				
							$_SESSION['usertype']= 'V';
							$_SESSION['customerid']= $row4['vendor_created_by'];
							$_SESSION['usertypeid']=$row4['usertypeid'];

							$url = "index.php"; // default page for
							
							header("Location: $url"); // perform correct redirect.

							exit;
						 }
						  else
						 { 
							header('Location:index.php?er=1');
						 }
					 }  
				}
			
				else
				{ 
						$query3  = "SELECT * FROM client_master where record_status='Y' and user_name='".trim($user)."'";
						$result3 = mysql_query($query3);
							if(mysql_num_rows($result3)!="")
							{ 
								while($row = mysql_fetch_assoc($result3))
								{

									if(trim(md5($password))==trim($row['password']))
										{ 
											$_SESSION['uid']= $row['client_id'];
											$_SESSION['name']= $row['client_name'];
											$_SESSION['password']= $row['password'];
											$_SESSION['usertype']= 'C';
											$_SESSION['username']=$row['user_name'];
											$_SESSION['customerid']=$row['client_created_by'];
											$_SESSION['usertypeid']=$row['usertypeid'];
											$_SESSION['usertypeid']=$row['usertypeid'];

											//if(isset($_SESSION['url'])) 
											  // $url = $_SESSION['url']; // holds url for last page visited.
											   //echo $url;
											//else 
											   $url = "index.php"; // default page for 

											header("Location: $url"); // perform correct redirect.

											exit;
										 } 
										 else
										 { 
											 header('Location:index.php?er=1');
										 }
									 }  
								}
							else
							{ 
							header('Location:index.php?er=2');
							}
				}
			}
}


ob_flush();
?>